package com.example.concurrentprogramming8016.future.futuretask;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @author liyanhao
 * @Date 2023-01-08 16:51
 */
public class FutureTaskDemo3 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask futureTask2 = new FutureTask<>(new T2Task());
        FutureTask<String> futureTask1
                = new FutureTask<>(new T1Task2(futureTask2));
        new Thread(futureTask2).start();
        new Thread(futureTask1).start();
        System.out.println(futureTask1.get());
    }
}

class T1Task implements Callable<String> {

    FutureTask<String> ft2;
    //t1任务需要t2任务的FutureTask

    public T1Task(FutureTask<String> ft2) {
        this.ft2 = ft2;
    }

    @Override
    public String call() throws Exception {
        System.out.println("T1:洗水壶");
        TimeUnit.SECONDS.sleep(1);
        System.out.println("T1:烧开水");
        TimeUnit.SECONDS.sleep(1);
        //获取T2线程的茶叶
        String s = ft2.get();
        System.out.println("T1:查到茶叶");
        System.out.println("T1:泡茶");
        return "上茶" + s;
    }
}

//T2Task需要执行的任务:洗茶壶 洗茶杯 拿茶叶
class T2Task implements Callable<String> {

    @Override
    public String call() throws Exception {
        System.out.println("T2:洗茶壶...");
        TimeUnit.SECONDS.sleep(1);

        System.out.println("T2:洗茶杯...");
        TimeUnit.SECONDS.sleep(2);

        System.out.println("T2:拿茶叶...");
        TimeUnit.SECONDS.sleep(1);
        return "龙井";
    }
}
